﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AddIn.EA.Architecture.Validation.BusinessEntity;
using AddIn.EA.Core.Common;
using AddIn.EA.Core.Interface;
using AddIn.EA.Framework;
using AddIn.EA.Framework.Common;
using AddIn.EA.Architecture.Validation.BusinessLogic;

namespace AddIn.EA.Architecture.Validation.Presentation
{
    [AddInMenu("Validar EA")]
    public partial class frmValidateSummary : Form, IAddInArchitecture
    {
        private RepositoryManager repository { get; set; }
        private IElement selectedElement { get; set; }

        public frmValidateSummary()
        {
            InitializeComponent();
        }

        #region IAddInArchitecture Members

        public bool CheckState(EA_Application.Repository repository, MenuLocation menuLocation)
        {
            this.repository = RepositoryManager.LoadManager(repository);

            this.selectedElement = this.repository.GetSelectedObject();

            bool showForm = CheckShowForm(selectedElement, menuLocation);

            if (!showForm)
                MessageBox.Show("O add-in selecionado deve ser executado apenas na package Arquitetura", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return showForm;
        }

        public void Execute(EA_Application.Repository repository, MenuLocation menuLocation)
        {
            IValidator validator = new Validator(this.repository, (Package)this.selectedElement);
            var errors = validator.Validate();

            if (!ShowSummary(errors, false))
                MessageBox.Show("Validação concluída com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion

        private bool CheckShowForm(IElement selectedElement, MenuLocation location)
        {
            return (selectedElement is AddIn.EA.Framework.Package &&
                    selectedElement.Name == "Arquitetura")
                    &&
                   (location == AddIn.EA.Core.Common.MenuLocation.MainMenu ||
                    location == AddIn.EA.Core.Common.MenuLocation.TreeView);
        }

        public bool ShowSummary(ValidateErrorList errors, bool isModal)
        {
            if (errors != null && errors.Count > 0)
            {
                dgvValidateSummary.DataSource = errors;
                dgvValidateSummary.Columns[0].HeaderText = "Elemento";
                dgvValidateSummary.Columns[1].HeaderText = "Erro";

                if (isModal)
                    this.ShowDialog();
                else
                    this.Show();

                return true;
            }
            else
            {
                this.Hide();
                return false;
            }
        }

        private void frmValidateSummary_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

    }
}
